package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.ljye.jg.ljye.R;

import java.util.ArrayList;

public class Activity_Withdrawals extends Activity {
    public static Button bt;//提现
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.bt://提现

                    break;
                case R.id.shuoming://提现说明
                    Intent intent = new Intent(Activity_Withdrawals.this, Activity_Withdrawals_Explain.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };
    private ImageView back;//返回
    private ListView listview;
    private TextView shuoming;//提现说明
    private ArrayList<String> listData;
    private SingleChoiceListViewAdapter mLvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawals);
        initView();
        initlistview();
        back.setOnClickListener(listener);
        shuoming.setOnClickListener(listener);

    }

    private void initlistview() {
        listData = new ArrayList<>();
        for (int i = 1; i <= 3; i++) {
            listData.add("建设银行" + i);
        }

        mLvAdapter = new SingleChoiceListViewAdapter(this, listData);
        listview.setAdapter(mLvAdapter);
        setListViewHeight(listview);
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        listview = (ListView) findViewById(R.id.lv_ew);
        shuoming = (TextView) findViewById(R.id.shuoming);
        bt = (Button) findViewById(R.id.bt);

    }

    //解决ScrollView包裹listview后只显示一个条目问题
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }

    static class SingleChoiceListViewAdapter extends BaseAdapter {

        public int temp = 0;//记录每次点击的按钮的Id 默认第一条
        private ArrayList<String> listData;
        private Activity activity;

        public SingleChoiceListViewAdapter(Activity activity, ArrayList<String> listData) {
            this.listData = listData;
            this.activity = activity;
        }

        @Override
        public int getCount() {
            return listData == null ? 0 : listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(activity).inflate(R.layout.listview_bank, null);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.name);
                viewHolder.radioButton = (RadioButton) convertView.findViewById(R.id.cb_item);
                viewHolder.main = (LinearLayout) convertView.findViewById(R.id.main);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.textView.setText(listData.get(position));
            viewHolder.radioButton.setId(position);//把RadioButton的Id设置为position
            viewHolder.radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {//如果是选中状态
                        if (temp != -1) {//temp不为-1，说明已经进行过点击事件
                            RadioButton tempButton = (RadioButton) activity.findViewById(temp);
                            if (tempButton != null) {
                                tempButton.setChecked(false);//取到上一次点击的RadioButton，并设置为未选中状态
                            }
                        }
                        temp = buttonView.getId();//将temp重新赋值，记录下本次点击的RadioButton
                    }
                }
            });

            if (position == temp) {
                viewHolder.radioButton.setChecked(true);//将本次点击的RadioButton设置为选中状态
            } else {
                viewHolder.radioButton.setChecked(false);
            }


            bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("SingleChoiceListViewAda", "选择id" + temp);
                }
            });

            return convertView;
        }

        final class ViewHolder {
            public TextView textView;
            public RadioButton radioButton;
            public LinearLayout main;
        }
    }

}
